﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using System.Net.Mail;
using System.Web.Script.Serialization;
using MapTest.Models;
using System.Reflection; 
//using MapTest.Models; // used for mail function

namespace MapTest.Controllers
{
    /// <summary>
    /// Contains all 
    /// </summary>

    public class HomeController : Controller
    {

        public string GetContacts()
        {
            var addressBook = new AddressBookEntity();
            var contacts = from contact in addressBook.UserInfoes
                           where contact.isAproovedForDisplay == true
                           select contact;

            var serializer = new JavaScriptSerializer();
            return serializer.Serialize(contacts); 
        }

        public string AddNewContact(UserInfo newUser)
        {
            // sets the new contact to be invisible until approved. 
            newUser.isAproovedForDisplay = false; 

            // Saves contact to Database. 
            var addressBook = new AddressBookEntity();
            addressBook.UserInfoes.Add(newUser);
            addressBook.SaveChanges(); 

            // emails administrator, to aprove the user
            Mail(newUser);
            
            return "testing";

        }

        private static void Mail(UserInfo newUser)
        {
            //System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            //message.To.Add("luckyperson@online.microsoft.com");
            //message.Subject = "This is the Subject line";
            //message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
            //message.Body = "This is the message body";
            //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
            //smtp.Send(message);
            // TODO: pull from web config file, and then remove web config file from repository. 
            var fromAddress = new MailAddress("", "");
            var toAddress = new MailAddress("", "Me");
            const string fromPassword = "";
            const string subject = "";
            string body = "";
            // TODO: format body of email with nice spacing ect, maybe use string builder class
            //this loop gets all the properties of the newUser object and adds their names and values to the body string. 
            foreach (var property in newUser.GetType().GetProperties())
            {
                body += property.Name;
                body += property.GetValue(newUser, null);
                //      body += property.GetValue();
            }
            // TODO: build uri using uri builder class and attach it to the body with the userid param?
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }
        }
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }


}
